﻿using System.Collections.Generic;
using System.Linq;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.web.wrokflows.DTOs;

namespace com.eforceglobal.crux.controllers.Administration
{
    public class EmailNotificationController
    {
        private readonly IEmailNotification _view;
        private EmailNotificationFacade _emailNotificationFacade;
       
        public EmailNotificationController(IEmailNotification view)
        {
            _view = view;
        }

        public void InitView(bool isPostBack)
        {
            if (!isPostBack)
            {
                PopulateNotificationUi();
            }
        }
        private void PopulateNotificationUi()
        {
            _emailNotificationFacade = new EmailNotificationFacade();
            var data = new SessionData();

            _view.EftNotificationStateModeSetList = _emailNotificationFacade.GetNotificationStateModeSetByUserId(data.CurrentUser.UserName);
            _view.EftNotificationModeList = _emailNotificationFacade.GetNotificationModes();
            _view.EftNotificationStateList = _emailNotificationFacade.GetEftNotificationStates();
        }
        public void HandleSaveNotificationModeStates()
        {
            _emailNotificationFacade = new EmailNotificationFacade();
            var data = new SessionData();
            var userName = data.CurrentUser.UserName;
            var stateModeSetInfoList = new List<NotificationStateModesInfo>();
            if (_view.EftNotificationStateList.Count == 0 || _view.EftNotificationModeList.Count == 0)
            {
                stateModeSetInfoList.Add(new NotificationStateModesInfo
                {
                    UserName = userName,
                    NotificationModeId = 0,
                    NotificationStateId = 0,
                });
            }
            else
            {
                stateModeSetInfoList = (from notificationState in _view.EftNotificationStateList
                                        from notificationMode in _view.EftNotificationModeList
                                        select new NotificationStateModesInfo
                                                   {
                                                       UserName = userName,
                                                       NotificationModeId = notificationMode.NotificationModeId,
                                                       NotificationStateId = notificationState.NotificationStateId
                                                   }).ToList();

            }
            _emailNotificationFacade.SaveModeState(stateModeSetInfoList);
            NavigationController.OpenView("Administration");
        }
        public void HandleCancel()
        {
            NavigationController.OpenView("Administration");
        }
    }
}
